﻿@using admin.Model
@{
    ViewData["Title"] = "UserEdit";
    Layout = "~/Views/Shared/_SmallWinLayout.cshtml";
    var user = Model as Customers;
}

<div class="page-container">
    <div class="form form-horizontal">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户ID：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@user.Cid"  id="userid" name="userid" readonly="readonly">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户名：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@user.Name" placeholder="" id="username" name="username">
                <span class="my_error" style="display:none">*用户名不能为空</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>性别：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@user.Sex" placeholder="" id="sex" name="sex" maxlength="1">
                <span class="my_error" style="display:none">*性别只能为"男"或"女"</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>年龄：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@user.Age" placeholder="" id="age" name="age" maxlength="3">
                <span class="my_error" style="display:none">*年龄格式错误</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>手机：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@user.Phone" placeholder="可选项" id="mobile" name="mobile" maxlength="11">
                <span class="my_error" style="display:none">*手机号格式错误</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>邮箱：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" placeholder="可选项" value="@user.Mail" name="email" id="email">
                <span class="my_error" style="display:none">*邮箱格式不正确</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">联系地址：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" placeholder="" value="@user.Address" name="address" id="address">
                <span class="my_error" style="display:none">*联系地址不能为空</span>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" onclick="userinfo('/Home/UserSaveInfo')" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    //提交用户信息
    function userinfo(url) {
        var infoList = new Array();
        $('input[name = userid]').blur(function () {
            infoList.push($(this).val());
        });
        $('input[name = username]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '用户名称不能为空');
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
            else {
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = sex]').blur(function () {
            val = this.value;
            if (val.match(/^(男|女)$/)) { 
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
            else {
                $(this).attr('placeholder', '性别填写不正确');
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
        });
        $('input[name = age]').blur(function () {
            val = this.value;
            if (val.match(/^([1-9][0-9]?|1[01][0-9]|120)$/)) {
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
            else {
                $(this).attr('placeholder', '年龄填写不正确');
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
        });
        $('input[name = mobile]').blur(function () {
            val = this.value;
            if (val.match(/^1[3|4|5|8|7]\d{9}$/) || val.length == 0) {
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
            else {
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
        });
        $('input[name = email]').blur(function () {
            val = this.value;
            if (val.match(/^\w+@@\w+\.\w+$/i) || val.length==0) {
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
            else {
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
        });
        $('input[name = address]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '联系地址不能为空');
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
            else {
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
        });
        layer.confirm('确认保存？', function (index) {
            infoList.length = 0;
            $('.auth').blur();
            tot = 0;
            $('.auth').each(function () {
                if ($(this).data('s') == 1) {
                    tot += 1;
                }
            });
            if (tot != 6) {
                //alert(tot);
                layer.msg('信息填写存在错误，请重新编辑', { icon: 5, time: 2000 });
            }
            else if (tot == 6) {
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: { "infoitem": infoList.toString() },
                    success: function (response) {
                        if (response == "ok") {
                            layer.msg('信息已保存', { icon: 1, time: 3000 });
                            self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                                window.parent.location.reload();
                            }, 2000); //这里2000代表两秒
                        }
                        else if (response == "nochange") {
                            layer.msg('信息没有改变哦', { icon: 1, time: 3000 });
                            self.setInterval(function () {  
                                //window.parent.location.reload();
                                layer_close();      
                            }, 2000); 
                        }
                        else if (response == "false") {
                            layer.msg('信息保存失败！请稍后再试', { icon: 5, time: 3000 });
                            infoList.length = 0;
                        }
                    },
                    error: function (response) {
                        layer.msg('信息保存失败！请重新编辑', { icon: 5, time: 3000 });
                        infoList.length = 0;
                    }
                });
            }

        });
    }
</script>